<template>
  <a-card :bordered="false">
    <a-form layout="inline">
      <a-row :gutter="48">
        <a-col :md="!advanced && 8 || 24" :sm="24">
          <span class="table-page-search-submitButtons">
            <a-button
              class="btn_add"
              type="primary"
              @click="$refs.addForm.add(stationId)"
              icon="plus">新增
            </a-button>
          </span>
        </a-col>
      </a-row>
    </a-form>
    <s-table
      id="table-wrapper"
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.id"
    >

      <span slot="timeSchedule" slot-scope="text">
        {{ getTime(text) }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
          <a class="btn_del">删除</a>
        </a-popconfirm>
      </span>
    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>

  </a-card>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizgatewayschedule/bizGatewayScheduleManage'
  import addForm from '../bizgatewayschedule/addForm'
  import editForm from '../bizgatewayschedule/editForm'
  import thresholdItemEdit from '../bizthreholditem/editForm'
  import thresholdItemAdd from '../bizthreholditem/addForm'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'

  export default {
    components: {

      STable,
      addForm,
      editForm,
      thresholdItemEdit,
      thresholdItemAdd
    },
    props: {
      stationId: {
        type: Number,
        require: false
      }
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 }
        },
        advanced: false, // 高级搜索 展开/关闭
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeLoading: true,
        queryParam: {},
        gatewayId: 0,
        columns: [ // 表头
          {
            title: '录音时段',
            dataIndex: 'timeSchedule',
            scopedSlots: { customRender: 'timeSchedule' }
          },
          {
            title: '网关名称',
            dataIndex: 'gatewayName'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: 150,
            scopedSlots: { customRender: 'action' },
            align: 'center'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },

    created () {
      this.queryParam.stationId = this.stationId
      const param = {
        stationId: this.stationId
      }
      gatewayList(param).then(res => {
        this.gatewayIds = []
        this.gatewayList = res.data
        for (let i = 0; i < res.data.length; i++) {
          this.gatewayIds.push(res.data[i].id)
        }
        this.queryParam.gatewayIds = this.gatewayIds.toString()
        try {
          this.$refs.table.refresh()
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      })
    },

    methods: {

      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      getTime (text) {
        var time = text.split('')
        var times = ''
        for (let i = 0; i < time.length; i++) {
          if (time[i] === '1') {
            times += [i] + '时，'
          }
        }
        var endTime = times.slice(0, times.length - 1)
        if (endTime.length === 0) {
          return '未录音'
        }
        return times.slice(0, times.length - 1)
      },
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      }
    }
  }
</script>

<style lang="less">
  @import '../~@/assets/css/list.less';

  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
>
